;;;=================================================================
;;;
;;; Subject: List of 1's and 0's to facets in pair form
;;;
;;;-----------------------------------------------------------------
;;;
;;; Jónathan Heras Vicente
;;; Beginning date: 26-04-10
;;;
;;;=================================================================


(defun coordinates-2d-to-facets-aux (coord)
  (append (list (list (list (car coord) (cadr coord)) 
                      (list (+ 1 (car coord)) (cadr coord))
                      (list (+ 1 (car coord)) (1+ (cadr coord)))))
          (list (list (list (car coord) (cadr coord)) 
                      (list (car coord) (1+ (cadr coord))) 
                      (list (+ 1 (car coord)) (1+ (cadr coord)))))
          )
  )


(defun coordinates-2d-to-facets (list)
  (do ((temp list (cdr temp))
       (temp2 nil))
      ((endp temp) temp2)
    (setf temp2 (append temp2 (coordinates-2d-to-facets-aux (car temp))))))



(defun list-to-facets-aux (list i j)
  (cond ((endp list) nil)
        ((equal (car list) 1) (append (list (list (list i j) (list (1+ i) j) (list (1+ i) (1+ j))))
                                      (list (list (list i j) (list i (1+ j)) (list (1+ i) (1+ j))))
                                      (list-to-facets-aux (cdr list) i (1+ j))))
        ((equal (car list) 0) (list-to-facets-aux (cdr list) i (1+ j)))))
                                    

(defun list-to-facets-aux1 (list i)
  (cond ((endp list) nil)
        (t (append (list-to-facets-aux (car list) i 0)
                   (list-to-facets-aux1 (cdr list) (1+ i))))))  


(defun 2D-to-facets (list)
  (transform-list-of-cons-pair (coordinates-2d-to-facets list)))



(defun genera-facets-i-j (i j)
  (list (list (list i j) (list (1+ i) j) (list (1+ i) (1+ j)))
	(list (list i j) (list i (1+ j)) (list (1+ i) (1+ j))))
  )


(defun genera-facets-list (list i j)
  (if (endp list)
      nil
    (if (equal (car list) 1)
	(append (genera-facets-i-j i j) 
		(genera-facets-list (cdr list) (1+ i) j))
      (genera-facets-list (cdr list) (1+ i) j))))


(defun genera-facets-image-aux (list j)
  (if (endp list)
      nil
    (append (genera-facets-list (car list) 0 j)
	    (genera-facets-image-aux (cdr list) (1+ j)))))

(defun generate-facets-image-2d (list)
  (genera-facets-image-aux list 0))



#|

Example:

(generate-facets-image-2d '((1 0) (0 1)))

|#
